<template>
  <div>
    <!-- 确保ID与你的图表对应 -->
    <div id="echartsyou2" class="charts"></div>
  </div>
</template>

<script>
import * as echarts from "echarts"; // 确保已经安装了echarts并正确导入

export default {
  data() {
    return {
      timer: null,
    };
  },
  mounted() {
    this.initEcharts();
    var numss = 1;
    this.timer = setInterval(() => {
      if (this.timer) {
        if (numss - 1 == 0) {
          this.initEcharts();
          numss = numss + 1;
        } else if (numss - 2 == 0) {
          this.initEcharts1();
          numss = numss + 1;
        } else if (numss - 3 == 0) {
          this.initEcharts2();
          numss = numss + 1;
        } else {
          this.initEcharts3();
          numss = numss - 3;
        }
      }
    }, 1500);

    window.addEventListener("resize", this.handleWindowResize);
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.handleWindowResize);
    clearInterval(this.timer);
    this.timer = null;
  },
  methods: {
    initEcharts() {
      const myChart = echarts.init(document.getElementById("echartsyou2"));
      // 基于准备好的dom，初始化echarts实例
      const option = {
        // backgroundColor: "#141f56",

        tooltip: {
          show: "true",
          trigger: "item",
          backgroundColor: "rgba(0,0,0,0.4)", // 背景
          padding: [8, 10], //内边距
          // extraCssText: 'box-shadow: 0 0 3px rgba(255, 255, 255, 0.4);', //添加阴影
          formatter: function (params) {
            if (params.seriesName != "") {
              return params.name + " ：  " + params.value + " 辆";
            }
          },
        },
        grid: {
          borderWidth: 0,
          top: 20,
          bottom: 35,
          left: 40,
          right: 10,
          textStyle: {
            color: "#fff",
          },
        },
        xAxis: [
          {
            type: "category",

            axisTick: {
              show: false,
            },

            axisLine: {
              show: true,
              lineStyle: {
                color: "rgba(255,255,255,0.2)",
              },
            },
            axisLabel: {
              inside: false,
              textStyle: {
                color: "#bac0c0",
                fontWeight: "normal",
                fontSize: "12",
              },
              // formatter:function(val){
              //     return val.split("").join("\n")
              // },
            },
            data: ["四川", "广州", "青岛", "福州", "武汉"],
          },
          {
            type: "category",
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
            splitArea: {
              show: false,
            },
            splitLine: {
              show: false,
            },
            data: ["四川", "广州", "青岛", "福州", "武汉"],
          },
        ],
        yAxis: {
          min: 10,
          type: "value",
          axisTick: {
            show: false,
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: "rgba(255,255,255,0.2)",
            },
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "rgba(255,255,255,0.1)",
            },
          },
          axisLabel: {
            textStyle: {
              color: "#bac0c0",
              fontWeight: "normal",
              fontSize: "12",
            },
            formatter: "{value}",
          },
        },
        series: [
          {
            type: "bar",
            itemStyle: {
              normal: {
                show: true,
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: "#00c0e9",
                  },
                  {
                    offset: 1,
                    color: "#3b73cf",
                  },
                ]),
                barBorderRadius: 50,
                borderWidth: 0,
              },
              emphasis: {
                shadowBlur: 15,
                shadowColor: "rgba(105,123, 214, 0.7)",
              },
            },
            zlevel: 2,
            barWidth: "20%",
            data: [23, 22, 20, 30, 22],
          },
          {
            name: "",
            type: "bar",
            xAxisIndex: 1,
            zlevel: 1,
            itemStyle: {
              normal: {
                color: "transparent",
                borderWidth: 0,
                shadowBlur: {
                  shadowColor: "rgba(255,255,255,0.31)",
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowOffsetY: 2,
                },
              },
            },
            barWidth: "20%",
            data: [30, 30, 30, 30, 30],
          },
        ],
      };
      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    },

    initEcharts1() {
      const myChart = echarts.init(document.getElementById("echartsyou2"));
      // 基于准备好的dom，初始化echarts实例
      const option = {
        // backgroundColor: "#141f56",

        tooltip: {
          show: "true",
          trigger: "item",
          backgroundColor: "rgba(0,0,0,0.4)", // 背景
          padding: [8, 10], //内边距
          // extraCssText: 'box-shadow: 0 0 3px rgba(255, 255, 255, 0.4);', //添加阴影
          formatter: function (params) {
            if (params.seriesName != "") {
              return params.name + " ：  " + params.value + " 辆";
            }
          },
        },
        grid: {
          borderWidth: 0,
          top: 20,
          bottom: 35,
          left: 40,
          right: 10,
          textStyle: {
            color: "#fff",
          },
        },
        xAxis: [
          {
            type: "category",

            axisTick: {
              show: false,
            },

            axisLine: {
              show: true,
              lineStyle: {
                color: "rgba(255,255,255,0.2)",
              },
            },
            axisLabel: {
              inside: false,
              textStyle: {
                color: "#bac0c0",
                fontWeight: "normal",
                fontSize: "12",
              },
              // formatter:function(val){
              //     return val.split("").join("\n")
              // },
            },
            data: ["四川", "广州", "青岛", "福州", "武汉"],
          },
          {
            type: "category",
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
            splitArea: {
              show: false,
            },
            splitLine: {
              show: false,
            },
            data: ["四川", "广州", "青岛", "福州", "武汉"],
          },
        ],
        yAxis: {
          min: 10,
          type: "value",
          axisTick: {
            show: false,
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: "rgba(255,255,255,0.2)",
            },
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "rgba(255,255,255,0.1)",
            },
          },
          axisLabel: {
            textStyle: {
              color: "#bac0c0",
              fontWeight: "normal",
              fontSize: "12",
            },
            formatter: "{value}",
          },
        },
        series: [
          {
            type: "bar",
            itemStyle: {
              normal: {
                show: true,
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: "#00c0e9",
                  },
                  {
                    offset: 1,
                    color: "#3b73cf",
                  },
                ]),
                barBorderRadius: 50,
                borderWidth: 0,
              },
              emphasis: {
                shadowBlur: 15,
                shadowColor: "rgba(105,123, 214, 0.7)",
              },
            },
            zlevel: 2,
            barWidth: "20%",
            data: [18, 40, 25, 35, 28],
          },
          {
            name: "",
            type: "bar",
            xAxisIndex: 1,
            zlevel: 1,
            itemStyle: {
              normal: {
                color: "transparent",
                borderWidth: 0,
                shadowBlur: {
                  shadowColor: "rgba(255,255,255,0.31)",
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowOffsetY: 2,
                },
              },
            },
            barWidth: "20%",
            data: [40, 40, 40, 40, 40],
          },
        ],
      };
      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    },

    initEcharts2() {
      const myChart = echarts.init(document.getElementById("echartsyou2"));
      // 基于准备好的dom，初始化echarts实例
      const option = {
        // backgroundColor: "#141f56",

        tooltip: {
          show: "true",
          trigger: "item",
          backgroundColor: "rgba(0,0,0,0.4)", // 背景
          padding: [8, 10], //内边距
          // extraCssText: 'box-shadow: 0 0 3px rgba(255, 255, 255, 0.4);', //添加阴影
          formatter: function (params) {
            if (params.seriesName != "") {
              return params.name + " ：  " + params.value + " 辆";
            }
          },
        },
        grid: {
          borderWidth: 0,
          top: 20,
          bottom: 35,
          left: 40,
          right: 10,
          textStyle: {
            color: "#fff",
          },
        },
        xAxis: [
          {
            type: "category",

            axisTick: {
              show: false,
            },

            axisLine: {
              show: true,
              lineStyle: {
                color: "rgba(255,255,255,0.2)",
              },
            },
            axisLabel: {
              inside: false,
              textStyle: {
                color: "#bac0c0",
                fontWeight: "normal",
                fontSize: "12",
              },
              // formatter:function(val){
              //     return val.split("").join("\n")
              // },
            },
            data: ["四川", "广州", "青岛", "福州", "武汉"],
          },
          {
            type: "category",
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
            splitArea: {
              show: false,
            },
            splitLine: {
              show: false,
            },
            data: ["四川", "广州", "青岛", "福州", "武汉"],
          },
        ],
        yAxis: {
          min: 10,
          type: "value",
          axisTick: {
            show: false,
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: "rgba(255,255,255,0.2)",
            },
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "rgba(255,255,255,0.1)",
            },
          },
          axisLabel: {
            textStyle: {
              color: "#bac0c0",
              fontWeight: "normal",
              fontSize: "12",
            },
            formatter: "{value}",
          },
        },
        series: [
          {
            type: "bar",
            itemStyle: {
              normal: {
                show: true,
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: "#00c0e9",
                  },
                  {
                    offset: 1,
                    color: "#3b73cf",
                  },
                ]),
                barBorderRadius: 50,
                borderWidth: 0,
              },
              emphasis: {
                shadowBlur: 15,
                shadowColor: "rgba(105,123, 214, 0.7)",
              },
            },
            zlevel: 2,
            barWidth: "20%",
            data: [50, 58, 30, 47, 20],
          },
          {
            name: "",
            type: "bar",
            xAxisIndex: 1,
            zlevel: 1,
            itemStyle: {
              normal: {
                color: "transparent",
                borderWidth: 0,
                shadowBlur: {
                  shadowColor: "rgba(255,255,255,0.31)",
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowOffsetY: 2,
                },
              },
            },
            barWidth: "20%",
            data: [60, 60, 60, 60, 60],
          },
        ],
      };
      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    },
    initEcharts3() {
      const myChart = echarts.init(document.getElementById("echartsyou2"));
      // 基于准备好的dom，初始化echarts实例
      const option = {
        // backgroundColor: "#141f56",

        tooltip: {
          show: "true",
          trigger: "item",
          backgroundColor: "rgba(0,0,0,0.4)", // 背景
          padding: [8, 10], //内边距
          // extraCssText: 'box-shadow: 0 0 3px rgba(255, 255, 255, 0.4);', //添加阴影
          formatter: function (params) {
            if (params.seriesName != "") {
              return params.name + " ：  " + params.value + " 辆";
            }
          },
        },
        grid: {
          borderWidth: 0,
          top: 20,
          bottom: 35,
          left: 40,
          right: 10,
          textStyle: {
            color: "#fff",
          },
        },
        xAxis: [
          {
            type: "category",

            axisTick: {
              show: false,
            },

            axisLine: {
              show: true,
              lineStyle: {
                color: "rgba(255,255,255,0.2)",
              },
            },
            axisLabel: {
              inside: false,
              textStyle: {
                color: "#bac0c0",
                fontWeight: "normal",
                fontSize: "12",
              },
              // formatter:function(val){
              //     return val.split("").join("\n")
              // },
            },
            data: ["四川", "广州", "青岛", "福州", "武汉"],
          },
          {
            type: "category",
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
            splitArea: {
              show: false,
            },
            splitLine: {
              show: false,
            },
            data: ["四川", "广州", "青岛", "福州", "武汉"],
          },
        ],
        yAxis: {
          min: 10,
          type: "value",
          axisTick: {
            show: false,
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: "rgba(255,255,255,0.2)",
            },
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "rgba(255,255,255,0.1)",
            },
          },
          axisLabel: {
            textStyle: {
              color: "#bac0c0",
              fontWeight: "normal",
              fontSize: "12",
            },
            formatter: "{value}",
          },
        },
        series: [
          {
            type: "bar",
            itemStyle: {
              normal: {
                show: true,
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: "#00c0e9",
                  },
                  {
                    offset: 1,
                    color: "#3b73cf",
                  },
                ]),
                barBorderRadius: 50,
                borderWidth: 0,
              },
              emphasis: {
                shadowBlur: 15,
                shadowColor: "rgba(105,123, 214, 0.7)",
              },
            },
            zlevel: 2,
            barWidth: "20%",
            data: [22, 30, 13, 45, 36],
          },
          {
            name: "",
            type: "bar",
            xAxisIndex: 1,
            zlevel: 1,
            itemStyle: {
              normal: {
                color: "transparent",
                borderWidth: 0,
                shadowBlur: {
                  shadowColor: "rgba(255,255,255,0.31)",
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowOffsetY: 2,
                },
              },
            },
            barWidth: "20%",
            data: [50, 50, 50, 50, 50],
          },
        ],
      };
      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    },

    handleWindowResize() {
      this.$nextTick(() => {
        this.myCharts.forEach((chart) => chart.resize());
      });
    },
  },
  data() {
    return {
      myCharts: [], // 用于存储所有echarts实例，以便在窗口调整大小时重新调整图表
    };
  },
};
</script>
